<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\api\service\OrderService;
use app\wechat\service\FansService;
use think\admin\Controller;
use think\facade\Db;
use Yansongda\Pay\Pay as SPay;

/**
 * Class Index
 * @package app\index\controller
 */
class Index extends Controller
{

    /**
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $user_info = session('user');
        if (empty($user_info)) {
            //$fans_info = WechatService::instance()->getWebOauthInfo($this->request->url(true), 1);
            //if (empty($fans_info['openid'])) $this->error('授权登录失败(001)~');
            //$user_info = FansService::instance()->get($fans_info['openid']);
            $user_info = FansService::instance()->get('oWFDH060hyB8L_-lhYwmEcFaR0ak');
            if (empty($user_info)) $this->error('授权登录失败(002)~');
            session('user', $user_info);
        }
        $id = $this->request->get('id', '', 'int');
        if (empty($id)) $this->error('缺少必要参数');
        $vo = Db::table('pt_group_act')->where(['id' => $id])->find();
        $now_time = time();
        $vo['act_start_time'] = strtotime($vo['act_start_time']);
        $vo['act_end_time'] = strtotime($vo['act_end_time']);
        if ($now_time < $vo['act_start_time']) {
            $vo['act_status'] = 1;
        } elseif ($now_time >= $vo['act_start_time'] && $now_time <= $vo['act_end_time']) {
            $vo['act_status'] = 2;
        } else {
            $vo['act_status'] = 3;
        }
        $vo['menu_list'] = Db::table('pt_menu')->where('id', 'in', $vo['menu_keys'])->select()->toArray();
        $vo['slider'] = explode('|', $vo['slider']);
        $this->assign('vo', $vo);
        $this->fetch();
    }

    /**
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
     */
    function orderPayNotify()
    {
        $wx_pay_config = config('base.wx_pay_config');
        $wx_pay_config['notify_url'] = "http://{$_SERVER['SERVER_NAME']}/index/index/orderPayNotify";
        $pay = SPay::wechat($wx_pay_config);
        try {
            $data = $pay->verify();
            Db::table('pt_test')->insert(['content' => json_encode($data)]);
            if ($data['result_code'] == 'SUCCESS') {
                $order_info = getOrderInfo(['top_no' => $data['out_trade_no']]);
                //微信支付成功，逻辑处理
                if ($order_info['pay_status'] == 0) {
                    $data['total_amount'] = $data['total_fee'] / 100;
                    $data['trade_no'] = $data['transaction_id'];
                    $data['pay_mode'] = 1;
                    $return_data = OrderService::instance()->doThirdPay($order_info, $data);
                    if ($return_data['status'] == -1) throw new \Exception($return_data['info']);
                }
            } else {
                throw new \Exception('error' . (is_string($data) ? $data : json_encode($data)));
            }
        } catch (\Exception $e) {
            //添加异常订单
            Db::table('pt_test')->insert(['content' => $e->getMessage()]);
        }
        return $pay->success()->send();
    }

    function push()
    {
        if (function_exists("shell_exec")) {
            $cute = "cd /home/wwwroot/pt.jampay.cn/ && git pull https://gitee.com/wojiaowuqi/group-work.git 2>&1";
            echo shell_exec($cute);
            die("下拉完成-" . date('Y-m-d H:i:s'));
        } else {
            die('系统配置：shell_exec函数不可用');
        }
    }
}